package ch.bfh.fbi.mobicomp.zap09;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Zap09 extends Activity {
	public static final String tag = "ZAP09";
	private EditText editText;
	private Button button;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.main);
		this.editText = (EditText) this.findViewById(R.id.editText1);
		this.button = (Button) this.findViewById(R.id.button1);
		this.button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				Zap09.this.sendBroadcast(Zap09.this.editText.getText()
						.toString());
			}
		});
	}

	private void sendBroadcast(final String command) {
		Log.d(Zap09.tag, "prepare to send: " + command);
		final String message = Zap09Service.ZAPPER_KEY_WORD + command;
		Log.d(Zap09.tag, "attaching RemoteZapperServiceKeyword: " + message);
		final Intent broadcastIntent = Zap09MessageBroadcastReceiver
				.getRemoteZapperMessageIntent(message);
		Log.d(Zap09.tag,
				"Received the following broadcastIntent from RemoteZapper: "
						+ broadcastIntent);
		if (broadcastIntent != null) {
			Log.d(Zap09.tag,
					"Sending the following broadcastIntent to RemoteZapper: "
							+ broadcastIntent);
			this.sendBroadcast(broadcastIntent);
			Log.d(Zap09.tag, "broadcast sent");
		}

	}
}